package com.fuqi.android.zhongcai.base.mvp.presenter;

import android.util.Log;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.base.mvp.model.UpdateVersionLogicImpl;
import com.fuqi.android.zhongcai.base.mvp.model.UpdateVersionLogicInter;
import com.fuqi.android.zhongcai.base.mvp.view.IAboutView;
import com.fuqi.android.zhongcai.base.request.api.VersionDetailApi;
import com.fuqi.android.zhongcai.base.request.result.VersionDetailResult;
import com.fuqi.android.zhongcai.utils.VersionUtils;
import com.fuqi.framework.network.http.RequestManager;

/**
 * Created by juner on 2015/12/24.
 */
public class AboutPresenterImpl implements AboutPresenterInter {

    private IAboutView iAboutView;
    private UpdateVersionLogicInter updateVersionLogic;

    public AboutPresenterImpl(IAboutView iAboutView) {
        this.iAboutView = iAboutView;
    }

    @Override
    public void checkNewVersion() {
        RequestManager.getInstance().call(new VersionDetailApi(new VersionDetailApi.VersionDetailParams(), respVersionListener, errorVersionListener));

    }

    @Override
    public void startUpdateVersion() {

        updateVersionLogic = new UpdateVersionLogicImpl();
        updateVersionLogic.startUpdateVersionService();

    }

    Response.Listener<VersionDetailResult> respVersionListener = new Response.Listener<VersionDetailResult>() {

        @Override
        public void onResponse(VersionDetailResult versionDetailresult) {

            Log.i("juner", "juner version code: " + VersionUtils.getVersionCode());
            if (VersionUtils.getVersionCode() < versionDetailresult.getInfo().getVersionId()) {

                iAboutView.hasNewVersion();

            } else {
                iAboutView.noNewVersion();
            }
        }
    };


    Response.ErrorListener errorVersionListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            iAboutView.checkNewVersionFailed();
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());
        }
    };

}
